import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  getConnection,
  Double,
  ColumnType,
  OneToMany,
  JoinTable,
  ManyToOne,
  Unique,
} from 'typeorm';
import { Exclude } from 'class-transformer';
import moment = require('moment');
import { Orders } from './order.entity';
import { Device } from './device.entity';

export enum UserRole {
  General = 'general',
  Admin = 'admin',
  Agent = 'agent',
}

export enum AdminRole {
  Admin = 'admin',
  Custom = 'custom',
  General = 'general',
}

@Entity()
export class User {
  @PrimaryGeneratedColumn({
    comment: 'id',
  })
  id: number;

  @OneToMany((type) => Orders, (Orders) => Orders.user)
  orders: Orders[];

  @OneToMany((type) => Device, (Device) => Device.user)
  devices: Orders[];

  @Column({
    nullable: true,
    comment: 'openId',
    length: 100,
  })
  openid: string;

  @Column({
    nullable: true,
    comment: 'phone',
    length: 15,
  })
  phone: string;

  @Column({
    nullable: true,
    comment: '用户名',
    length: 50,
    default: '',
  })
  name: string;

  @Column({
    nullable: true,
    comment: '头像url',
    length: 1000,
    default: '',
  })
  avatar: string;

  @Exclude()
  @Column({
    nullable: true,
    comment: '密码',
    length: 100,
  })
  password: string;

  @Column({
    type: 'enum',
    enum: AdminRole,
    nullable: false,
    comment: 'admin:超级管理员， custom:客户， general: 普通用户',
    default: AdminRole.General,
  })
  role: AdminRole;

  @Column({
    type: 'enum',
    enum: UserRole,
    nullable: false,
    comment: 'general:普通用户 person_vip:个人vip, business_vip:企业vip',
    default: UserRole.General,
  })
  userRole: UserRole;

  @Column({
    type: 'decimal',
    nullable: true,
    comment: '短信金额',
    precision: 7,
    scale: 2,
  })
  smsMoney: number;

  @Column({
    type: 'decimal',
    nullable: true,
    comment: '电话金额',
    precision: 7,
    scale: 2,
  })
  phoneMoney: number;

  @CreateDateColumn({
    type: 'timestamp',
    nullable: false,
    comment: 'createAt',
    update: false,
    transformer: {
      to: (value) => {
        return value;
      },
      from: (value) => {
        return moment(value).format('YYYY-MM-DD HH:mm:ss');
      },
    },
  })
  creatTime: Date;

  @CreateDateColumn({
    type: 'timestamp',
    nullable: false,
    comment: 'updateAt',
    update: true,
    transformer: {
      to: (value) => {
        return value;
      },
      from: (value) => {
        return moment(value).format('YYYY-MM-DD HH:mm:ss');
      },
    },
  })
  updateTime: Date;
}
